package com.wuhan.template.module.wode.fragment;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.wuhan.template.R;
import com.wuhan.template.base.BaseFragment;
import com.wuhan.template.module.find.webview.WebViewActivity;
import com.wuhan.template.module.home.activity.HomeSearchActivity;
import com.wuhan.template.module.wode.activity.MyCollectActivity;
import com.wuhan.template.module.wode.activity.SheZhiActivity;
import com.wuhan.template.module.wode.activity.TodayImageActivity;
import com.wuhan.template.module.wode.service.DownLoadService;
import com.wuhan.template.views.CheckVersionUtils;
import com.wuhan.template.views.IDownload;
import com.xys.libzxing.zxing.activity.CaptureActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/13 0013.
 */
public class WoDeFragment extends BaseFragment{

    @BindView(R.id.btn_myCollect)
    protected RelativeLayout myCollect;

    @BindView(R.id.btn_today_image)
    protected RelativeLayout image;

    @BindView(R.id.btn_erweima)
    protected RelativeLayout erWeiMa;

    @BindView(R.id.btn_checkUptate)
    protected RelativeLayout checkUpdate;

    @BindView(R.id.btn_search)
    protected RelativeLayout search;

    @BindView(R.id.btn_jifeng_shangcheng)
    protected RelativeLayout shangCheng;

    @BindView(R.id.btn_jifeng_rule)
    protected RelativeLayout rule;

    @BindView(R.id.btn_chat_our)
    protected RelativeLayout lianXi;

    @BindView(R.id.btn_shezhi)
    protected RelativeLayout sheZhi;


    public WoDeFragment(){}


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wode;
    }

    @Override
    protected void initViews() {
        myCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MyCollectActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        image .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), TodayImageActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        erWeiMa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CaptureActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), HomeSearchActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        shangCheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("url","http://mobile.appwuhan.com/h5/jf_mall/" );
                intent.putExtra("title", "积分商城");
                startActivity(intent);
            }
        });

        rule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("url", "http://www.appwuhan.com/folder46/2015-05-18/43168.html");
                intent.putExtra("title", "专题");
                startActivity(intent);
            }
        });

        lianXi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("url", "http://www.appwuhan.com/folder46/2015-12-30/67997.html");
                intent.putExtra("title", "专题");
                startActivity(intent);
            }
        });

        sheZhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), SheZhiActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

    }

    private CheckVersionUtils versionUtils;
    @OnClick(R.id.btn_checkUptate)
    public void checkUpdate(View view) {

       // Toast.makeText(getContext(), "检测新版中,请稍后...", Toast.LENGTH_SHORT).show();
        new Thread(){
            @Override
            public void run() {
                versionUtils = new CheckVersionUtils(getContext());
                boolean needUpdate = versionUtils.isNeedUpdate();
                if(needUpdate){
                    Message message = handler.obtainMessage();
                    message.obj = needUpdate;
                    handler.sendMessage(message);
                }
            }
        }.start();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            boolean needUpdate = (boolean) msg.obj;
            if(needUpdate){
                showUpdateDialog();
            }
            else {
                Toast.makeText(getContext(), "没有检测到新版本", Toast.LENGTH_SHORT).show();
            }
        }
    };


    private AlertDialog dialog;
    private AlertDialog.Builder builder;
    private void showUpdateDialog(){

        if(dialog==null){
        builder = new AlertDialog.Builder(getContext());
        builder.setTitle("版本更新")
        .setMessage("检测到新版本,是否进行更新?")
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent intent = new Intent(getContext(), DownLoadService.class);
                getContext().startService(intent);
                getContext().bindService(intent,connection, DownLoadService.BIND_AUTO_CREATE);
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
         dialog = builder.create();
    }
        dialog.show();
    }

    private boolean isBounded = false;
    private IDownload download;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            download = (IDownload) service;
            isBounded = true;
            download.download();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
}
